unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Sockets, StdCtrls, ShellAPI, ExtCtrls;

  procedure BackUpOldFile();
  procedure DownloadNewFile();
  var done : boolean;
type


  TForm1 = class(TForm)
    Label1: TLabel;
    TcpClient_Update: TTcpClient;
    Timer1: TTimer;
    Timer2: TTimer;
    procedure FormCreate(Sender: TObject);

    procedure Timer1Timer(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

    type
  TUpdate = class(TThread)
  private
    { Private declarations }
  protected
    procedure Execute; override;
  end;


var
  Form1: TForm1;
  date_now, time_now : string;
  time_convert : variant;
  programfile : string;

implementation

{$R *.dfm}

procedure DownloadNewFile();
var
temp: string;
temp2: string;
tempint : integer;
datatemp : byte;
doorgaan : boolean;
newversion : string;
counter : integer;
outputfile : file;
convert : variant;
filesize : integer;
begin

form1.TcpClient_Update.RemoteHost:='blaatschaap.nukysrealm.net';
form1.TcpClient_Update.RemotePort:='80';
if form1.TcpClient_Update.Connect then
  begin
  form1.TcpClient_Update.Sendln('GET /blaatbot/IRCbot.exe HTTP/1.1');   ;
  form1.TcpClient_Update.Sendln('user-agent: ' + form1.caption);
  form1.TcpClient_Update.Sendln('host: '+ form1.TcpClient_Update.RemoteHost );
  form1.TcpClient_Update.Sendln('connection: close');
  form1.TcpClient_Update.Sendln('');
  doorgaan := false;
  temp:=form1.TcpClient_Update.Receiveln();

  temp2 := temp[10]+temp[11]+temp[12]+temp[13]+temp[14]+temp[15];
  if temp2 = '200 OK' then doorgaan := true ;
  repeat

  if length(temp) > 16 then
    begin
//    showmessage(temp);
    if (temp[1]= 'C' ) AND ( temp[2]= 'o' ) AND ( temp[3]= 'n' ) AND ( temp[4]= 't'
    ) AND ( temp[5]= 'e' ) AND ( temp[6]= 'n' ) AND ( temp[7]= 't' ) AND ( temp[8]= '-'
    ) AND ( temp[9]= 'L' ) AND ( temp[10]='e' ) AND ( temp[11]='n' ) AND ( temp[12]='g'
    ) AND ( temp[13]='t' ) AND ( temp[14]='h' ) AND ( temp[15]=':' ) AND ( temp[16]= ' ') then
    begin
//          form1.Label1.Caption:=('updating');
          counter := 17;
          temp2:='';
          repeat
          temp2:=temp2+temp[counter];
          counter := counter + 1;
          until counter-1 = length(temp);
          convert := temp2;
          filesize :=convert;
          end;

       end;

  temp:=form1.TcpClient_Update.Receiveln();
  until temp =''; // header
  if doorgaan then
      begin

      counter :=0;
      tempint :=0;
      assign ( outputfile, 'IRCbot.exe' );
      rewrite (outputfile,1);
      repeat
     counter := counter + 1;
     tempint := counter;
      Form1.TcpClient_Update.ReceiveBuf(datatemp,1);
      BlockWrite(outputfile, datatemp, 1);
      until counter = filesize +1 ;
      closefile(outputfile);
      form1.Label1.Caption:='Finished Downloading '+ temp2 + ' bytes';


      form1.TcpClient_Update.Disconnect;
      end;
  end;
end;




procedure BackUpOldFile();
var
inputfile: file;
         outputfile : file;
         counter,counter2: integer;
temp : byte;
begin
try
assign ( inputfile  , 'IRCbot.exe');

assign ( outputfile , 'IRCbot_backup_'+date_now+'_'+time_now+'.exe');
 reset   (inputfile,1);
rewrite (outputfile,1);


      while not eof(inputfile) do


      begin

//read  (inputfile) ;
//write ( outputfile );
// woorks fine for text file but not for data files

BlockRead(inputfile, temp, 1, counter2);
BlockWrite(outputfile, temp, counter2);

      end;
      closefile(inputfile);
      closefile(outputfile);
form1.Label1.Caption:='IRCbot.exe backupped'
except form1.Label1.Caption:='IRCbot.exe file error'; programfile:='updater.exe';

end;

end;


procedure TForm1.FormCreate(Sender: TObject);

begin
done := false;
 programfile := 'IRCbot.exe';
// set date and time formats
TimeSeparator   := '-';
DateSeparator   := '-';
ShortDateFormat := 'dd/mm/yyyy' ;
LongTimeFormat  := 'hh:nn:ss'   ;
timer1.Enabled:=true 
end;

procedure TUpdate.execute;
begin
form1.timer1.Enabled:=false;
date_now :=DateToStr(date);
time_now :=TimeToStr(time);
form1.Label1.Caption:='Backupping Blaatbot';
form1.Repaint;
BackupOldFile();
form1.Label1.Caption:='Downloading Blaatbot';
form1.Repaint;
DownloadNewFile();
form1.Label1.Caption:='Restarting Blaatbot';
form1.Repaint;
done := true;

end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
 Form1.BorderIcons:=[];
TUpdate.Create(false);
end;
procedure TForm1.Timer2Timer(Sender: TObject);
begin
if done then begin

ShellExecute(Handle, 'open', PChar(programfile), nil, nil, SW_SHOWNORMAL);
Form1.Close;
end;

end;

end.
